package com.devilvan.betrice.houseprice.component;

import com.devilvan.betrice.common.BetriceCommon;
import com.devilvan.betrice.cache.BetriceRedisHelper;
import com.devilvan.betrice.houseprice.constant.HousePriceRedisKeyConstant;
import com.devilvan.betrice.util.UEmpty;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author Evad.Wu
 * @Description 房价信息 使用Redis组件类
 * @date 2022-07-17
 */
@Component
public class HousePriceRedisHelper extends BetriceRedisHelper {
    /**
     * 贝壳新房的页数会在第19页时，由10条变为20条，因此页数需要重新计算
     */
    public static final int PAGE_SIZE_SEPARATE = 18;
    public static final int PAGE_SIZE_10 = 10;
    public static final int PAGE_SIZE_20 = 20;

    @Resource(name = "betriceRedisTemplate")
    private RedisTemplate<String, Object> redisTemplate;

    public Map<Object, Object> generateRedisHeaderDto(HousePriceRedisKeyConstant housePriceRedisKeyConstant) {
        String name = housePriceRedisKeyConstant.name();
        Map<Object, Object> entries = redisTemplate.opsForHash().entries(name);
        if (UEmpty.isEmpty(entries)) {
            Map<String, String> map = new HashMap<>(2);
            map.put(BetriceCommon.SUFFIX_TOTAL_PAGE, checkRedisTotalPage(name).toString());
            map.put(BetriceCommon.SUFFIX_CURRENT_PAGE, checkRedisCurrentPage(name).toString());
            redisTemplate.opsForHash().putAll(name, map);
            entries.putAll(map);
        }
        return entries;
    }

    /**
     * 检查并修改Redis中该总页数key的值
     *
     * @param totalPg    本次查询得到的总数据量
     * @param totalCount 上游查询到的总页数
     * @param redisKey   待修改的总页数key
     * @return 修改后的总页数
     */
    @Override
    public Integer checkSaveRedisTotalPage(Integer totalPg, Integer totalCount, String redisKey) {
        // 判断上游查到的Redis中的总页数 是否为初始值，若是则用第一次查询到总页数重置
        if (BetriceCommon.INIT_TOTAL_PAGE.equals(totalPg)) {
            // 贝壳新房的页数会在第19页时，由10条变为20条，因此页数需要重新计算
            if (totalCount <= PAGE_SIZE_SEPARATE * PAGE_SIZE_10) {
                totalPg = totalCount % PAGE_SIZE_10 == 0 ?
                        totalCount / PAGE_SIZE_10 : (totalCount / PAGE_SIZE_10) + 1;
            } else {
                int front = PAGE_SIZE_SEPARATE * PAGE_SIZE_10;
                int back = totalCount - front;
                totalPg = (front / PAGE_SIZE_10) + (back % PAGE_SIZE_20 == 0 ?
                        back / PAGE_SIZE_20 : (back / PAGE_SIZE_20) + 1);
            }

            redisTemplate.opsForHash().put(redisKey, BetriceCommon.SUFFIX_TOTAL_PAGE, totalPg.toString());
        }
        return totalPg;
    }

    /**
     * 根据current和total生成请求的URL
     *
     * @param url       处理前的请求URL
     * @param totalPg   总页数
     * @param currentPg 当前页数
     * @return 待页数参数的URL
     */
    @Override
    public String generatePageUrl(String url, Integer totalPg, Integer currentPg) {
        String pgUrl = null;
        // 如果总页数没有被初始化
        if (BetriceCommon.INIT_TOTAL_PAGE.equals(totalPg)) {
            pgUrl = url;
        } else if (currentPg <= totalPg) {
            // 有页数的情况
            pgUrl = url + "pg" + currentPg + "/";
        }
        return pgUrl;
    }
}
